from models.base import BaseModel, db
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash


class SystemUser(UserMixin, BaseModel):
    __tablename__ = 'system_user'
    __table_args__ = {'comment': 'CMS管理员表'}

    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    username = db.Column(db.String(50), nullable=False, unique=True, comment='用户名')
    password_hash = db.Column(db.String(255), nullable=False, comment='密码')
    real_name = db.Column(db.String(50), nullable=False, comment='真实姓名')
    role = db.Column(db.String(50), nullable=False, comment='角色')
    status = db.Column(db.String(50), nullable=False, comment='状态')

    @property
    def password(self):
        return self.password_hash

    @password.setter
    def password(self, orig_password):
        self.password_hash = generate_password_hash(orig_password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)
